<?php
// $Id: BinaryValue.class.php 73 2007-02-12 03:45:43Z tswicegood $

/**
 * This file contains {@link BinaryValue} which is part of the PHP Content Repository
 * (phpCR), a derivative of the Java Content Repository JSR-170,  and is 
 * licensed under the Apache License, Version 2.0.
 *
 * This file is based on the code created for
 * {@link http://www.jcp.org/en/jsr/detail?id=170 JSR-170}
 *
 * @author Travis Swicegood <travis@domain51.net>
 * @copyright PHP Code Copyright &copy; 2004-2005, Domain51, United States
 * @copyright Original Java and Documentation 
 *  Copyright &copy; 2002-2004, Day Management AG, Switerland
 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, 
 *      Version 2.0
 * @package phpContentRepository
 */


/**
 * Load the {@link phpCR} library file(s)
 */ 
require_once(dirname(__FILE__) . '/phpCR.library.php');

phpCR::loadAbstract('BaseValue');
phpCR::loadClass('PropertyType');


/**
 * A {@link BinaryValue} provides an implementation of the {@link Value}
 * interface representing a binary value.
 *
 * @author PHP - Travis Swicegood <travis@domain51.net>
 * @copyright Copyright &copy; 2004-2005, Domain51
 * @package phpContentRepository
 */
class BinaryValue extends BaseValue
{
   /**
    * Sets this class' type
    *
    * @var int
    */
    const TYPE = PropertyType::BINARY;

    
   /**
    * Handles initialization
    *
    * <b>PHP Note</b>: Since PHP does not have method overloading like Java,
    * this method has to take into consideration multiple types of data in
    * $value.
    *
    * @param string|resource
    *   This has to be either a string or a resource pointing to a stream such
    *   as {@link ValueStream}
    */
    public function __construct($value)
    {
        assert('is_string($value) || is_resource($value)');

        parent::__construct(self::TYPE);
        $this->data = $value;
    }
    
    
   /**
    * Indicates whether some other object is "equal to" this one.
    *
    * The result is TRUE if and only if the argument is not
    * NULL and is a {@link BinaryValue} object that represents the
    * same value as this object.
    *
    * @param object
    *   The reference object with which to compare.
    * @return bool
    *   TRUE if this object is the same as the $obj
    *   argument; FALSE otherwise.
    */
    public function equals($obj)
    {
        assert('is_object($obj)');
        
        if ($this == $obj) {
            return true;
        }
        
        if ($obj instanceof BinaryValue) {
            if ($this->getData() == $obj->getData()) {
                return true;
            }
        }
        
        return false;
    }
    
    
   /**
    * @see Value::getDate()
    */
    public function getDate()
    {
        $this->setValueConsumed();
        $date = date($this->getData(), 'c');
        
        if (!is_null($date)) {
            return $date;
        } else {
            phpCR::loadException('ValueFormat');
            throw new ValueFormatException('not a valid date format');
        }
    }
    
    
   /**
    * @see Value::getStream()
    */
    public function getStream()
    {
        $this->setStreamConsumed();
        
        // If this is already a resource, just return that
        if (is_resource($this->data)) {
            return $this->data;
        }
        
        return parent::getStream();
    }
    
    
   /**
    * @see Value::getString()
    */
    public function getString()
    {
        $this->setValueConsumed();
        
        // Read and return the appropriate value
        switch(gettype($this->data)) {
        case 'resource':
            return fread($this->data, filesize($this->data));
            break;
        
        case 'array':
        case 'object':
            phpCR::loadException('ValueFormat');
            throw new ValueFormatException(sprintf(
                'conversion from %s to string failed', gettype($this->data)));
            break;
        
        case 'boolean':
        case 'double':
        case 'integer':
        case 'string':
            $string = $this->data;
            settype($string, 'string');
            
            return $string;
            break;
        
        case 'NULL':
        default:
            phpCR::loadException('ValueFormat');
            throw new ValueFormatException('empty value');
        }
    }
}

